package com.turtle.learning.contents.controller;

import com.turtle.learning.contents.dto.AuditDTO;
import com.turtle.learning.contents.dto.ShareDTO;
import com.turtle.learning.contents.entity.Share;
import com.turtle.learning.contents.service.impl.ShareServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 分享表
 *
 * @author turtle
 * @since： 2020-04-26
 */
@RestController
@Api(value = "分享表", tags = "分享表")
@RequestMapping("/contents/share")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))

public class ShareController {
    private final ShareServiceImpl shareService;
//    private final RocketMQTemplate rocketMQTemplate;

    /**
     * 获取分享列表
     *
     * @return List<Share> 文章列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取分享列表")
    public List<Share> getShareList() {
        return shareService.list();
    }

    /**
     * 添加分享
     *
     * @param shareDTO 文章实体
     * @return boolean
     */
    @PostMapping("/add")
    @ApiOperation(value = "添加分享")
    public boolean addShare(@Validated @RequestBody ShareDTO shareDTO) {
        Share share = Share.builder()
            .author(shareDTO.getAuthor())
            .cover(shareDTO.getCover())
            .title(shareDTO.getTitle())
            .summary(shareDTO.getSummary())
            .isOriginal(shareDTO.getIsOriginal())
            .downloadUrl(shareDTO.getDownloadUrl())
            .price(shareDTO.getPrice())
            .userId(shareDTO.getUserId())
            .build();
        return shareService.save(share);

    }

    /**
     * 审核文章
     *
     * @param id 文章id
     * @return Share 文章
     */
    @PutMapping("/audit/{id}")
    @ApiOperation(value = "审核文章")
    public Share auditShare(@PathVariable("id") Integer id, @Validated @RequestBody AuditDTO auditDTO) {
        return shareService.auditById(id, auditDTO);
    }

}